package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.AssistStatus;
import cn.fintecher.pangolin.common.enums.CaseDataStatus;
import cn.fintecher.pangolin.common.model.DepartmentModel;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.PaymentRecord;
import cn.fintecher.pangolin.entity.elastic.PaymentRecordModel;
import cn.fintecher.pangolin.entity.mysql.AssistCase;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.dataimp.elastic.BaseCaseElastic;
import cn.fintecher.pangolin.service.dataimp.repository.AssistCaseRepository;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:12 2019/11/5
 */

@Service("confirmBillFuture")
public class ConfirmBillFuture {
    @Autowired
    private BaseCaseElastic baseCaseElastic;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private AssistCaseRepository assistCaseRepository;

    /**
     * 多线程确认
     */
    @Async
    public CompletableFuture<List<PaymentRecord>> processBill(List<PaymentRecordModel> list, UserModel userModel, DepartmentModel departmentModel) {
        List<String> caseNumbers = new ArrayList<>();
        List<PaymentRecord> paymentRecords = new ArrayList<>();
        for (PaymentRecordModel paymentRecordModel : list) {
            caseNumbers.add(paymentRecordModel.getCaseNumber());
        }
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.termsQuery("caseNumber.keyword", caseNumbers));
        Iterable<BaseCase> iterable = baseCaseElastic.search(builder);
        Iterable<CaseInfo> caseInfoIterable = caseInfoRepository.findAll(QCaseInfo.caseInfo.caseNumber.in(caseNumbers).and(QCaseInfo.caseInfo.caseDataStatus.ne(CaseDataStatus.PAUSE)));
        Iterable<AssistCase> assistCaseIterable = assistCaseRepository.findAll(QAssistCase.assistCase.caseNumber.in(caseNumbers).and(QAssistCase.assistCase.assistStatus.eq(AssistStatus.ASSIST_COLLECTING)));
        List<BaseCase> baseCases = IterableUtils.toList(iterable);
        List<CaseInfo> caseInfos = IterableUtils.toList(caseInfoIterable);
        List<AssistCase> assistCases = IterableUtils.toList(assistCaseIterable);
        for (PaymentRecordModel paymentRecordModel : list) {
            PaymentRecord paymentRecord = new PaymentRecord();
            paymentRecord.setId(paymentRecordModel.getId()); //主键ID,和导入模型ID保持一致,方便撤销操作
            paymentRecord.setCaseNumber(paymentRecordModel.getCaseNumber()); //案件编号
            paymentRecord.setPrincipalId(paymentRecordModel.getPrincipalId()); //委托方ID
            paymentRecord.setPrincipalName(paymentRecordModel.getPrincipalName()); //委托方名称
            paymentRecord.setPaymentAmt(paymentRecordModel.getPaymentAmt()); //还款金额
            paymentRecord.setPaymentDate(paymentRecordModel.getPaymentDate()); //还款日期
            paymentRecord.setRemark(paymentRecordModel.getRemark()); //备注
            paymentRecord.setPersonalName(paymentRecordModel.getPersonalName()); //客户姓名
            paymentRecord.setCardNo(paymentRecordModel.getCardNo()); //卡号
            paymentRecord.setBillType(paymentRecordModel.getBillType()); //账单类型
            for (BaseCase baseCase : baseCases) {
                if (paymentRecordModel.getCaseNumber().equals(baseCase.getCaseNumber())) {
                    paymentRecord.setCaseId(baseCase.getId());
                    paymentRecord.setBatchNumber(baseCase.getBatchNumber());
                    paymentRecord.setCertificateNo(baseCase.getCertificateNo());
                    paymentRecord.setStage(baseCase.getStage());
                    paymentRecord.setBeforeLeftAmt(baseCase.getLeftAmt());
                    break;
                }
            }
            for (CaseInfo caseInfo : caseInfos) {
                if (paymentRecordModel.getCaseNumber().equals(caseInfo.getCaseNumber())) {
                    paymentRecord.setCurrentCollector(Objects.nonNull(caseInfo.getCurrentCollector()) ? caseInfo.getCurrentCollector().getId() : null);
                    break;
                }
            }
            for (AssistCase assistCase : assistCases) {
                if (paymentRecord.getCaseNumber().equals(assistCase.getCaseNumber())) {
                    paymentRecord.setAssistCollector(Objects.nonNull(assistCase.getAssistCollector()) ? assistCase.getAssistCollector().getId() : null);
                    break;
                }
            }
            paymentRecord.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
            paymentRecords.add(paymentRecord);
        }
        return CompletableFuture.completedFuture(paymentRecords);
    }
}
